export function createStyleSheet() {
  const style = document.createElement('style')
  style.appendChild(document.createTextNode(''))
  document.head.appendChild(style)
  return style.sheet
}

const stylesheet = createStyleSheet()

export function addRule(selector, css) {
  const propText = typeof css === 'string' ? css : Object.keys(css).map(function(p) {
    return p + ':' + (p === 'content' ? "'" + css[p] + "'" : css[p])
  }).join(';')

  if (stylesheet.insertRule) {
    // 标准浏览器支持的
    stylesheet.insertRule(selector + '{' + propText + '}', stylesheet.cssRules.length)
  } else if (stylesheet.addRule) {
    // IE支持的
    stylesheet.addRule(selector, propText, stylesheet.cssRules.length)
  }
}
